/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Datos;

import Entidades.Modelo;
import Interfaces.IModelo;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author HOUSE
 */
public class DBModelo implements IModelo{
    private Connection conexion = null;
    public Connection getConection() {
       if (conexion == null) {
           try {
               Driver driver = new com.mysql.jdbc.Driver();
               DriverManager.registerDriver(driver);
               conexion = DriverManager.getConnection("jdbc:mysql://localhost:3306/automotora", "root", "pass1234");
              
           } catch (SQLException ex) {
               ex.printStackTrace();
               Logger.getLogger(DBAutos.class.getName()).log(Level.SEVERE, null, ex);

           }
       }
       return conexion;
   }

    @Override
    public boolean IngresaModelo(Modelo nodo) {
        try {
            CallableStatement statement;
            statement = getConection().prepareCall("{call IngresaModelo (?,?,?)}");
            statement.setInt(1, nodo.getIDModelo());
            statement.setInt(2, nodo.getIDMarca());
            statement.setString(3,nodo.getNombre());



            statement.executeUpdate();
        } catch (Exception ex) {
            System.out.println("error al agregar modelo");
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean ModificaModelo(Modelo nodo) {
        try {
            CallableStatement statement;
            statement = getConection().prepareCall("{call ModificaModelo(?,?,?)}");
            statement.setInt(1, nodo.getIDModelo());
            statement.setInt(2,nodo.getIDMarca() );
            statement.setString(3, nodo.getNombre());



            statement.executeUpdate();
        } catch (Exception ex) {
            System.out.println("error al modificar marca");
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean EliminaModelo(int IDModelo) {
        try {
            CallableStatement statement;
            statement = getConection().prepareCall("{call EliminaModelo(?)}");
            statement.setInt(1, IDModelo);
            statement.executeUpdate();


        } catch (Exception ex) {
            System.out.println("error al eliminar modelo");
            ex.printStackTrace();
            return false;
        }
        return true;
    }
    

    @Override
    public ArrayList<Modelo> ListaModelo() {
        ArrayList<Modelo> per = new ArrayList<Modelo>();
       try {
           CallableStatement statement = getConection().prepareCall("{call ListaModelo()}");
           ResultSet resultSet = statement.executeQuery();
           while (resultSet.next()) {
               Modelo reglam= new Modelo();
              
               reglam.setIDModelo(resultSet.getInt(1));
               reglam.setIDMarca(resultSet.getInt(2));
               reglam.setNombre(resultSet.getString(3));
               
               
               per.add(reglam);
              
           }
           return per;
       } catch (SQLException ex) {
           ex.printStackTrace();
           return null;
       }
    }

    @Override
    public DefaultTableModel ModeloModelo() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
}
